//
//  WebPageVC.m
//  ZhiHuDaily
//
//  Created by 吴宜昊 on 2022/2/6.
//

#import "WebPageVC.h"
#import <WebKit/WebKit.h>
#import "WebPageNavBar.h"
#import "WebPageDelegate.h"

@interface WebPageVC () <WKNavigationDelegate , WebPageDelegate , UIScrollViewDelegate >
@property(nonatomic,strong) UIImageView * ivDefault;
@property(nonatomic,strong) WKWebView * webView;
@property(nonatomic,strong) NSString * strURL;
@property(nonatomic,strong) WebPageNavBar * navBar;
@end

@implementation WebPageVC {
    CGFloat contentOffsetY;
}

#pragma mark - init
-(instancetype)initWithURL:(NSString *) strURL {
    self = [super init];
    if (self) {
        contentOffsetY = 0.f;
        self.strURL = [strURL stringByReplacingOccurrencesOfString:@"%s" withString:@"%25s"];
        self.allowPopGesture = YES;
    }
    return self;
}

#pragma mark - override
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    [self requestURL];
}

-(void) updateViewConstraints {
    [self remakeMas];
    [super updateViewConstraints];
}

#pragma mark - setupUI
-(void) setupUI {
    [self.view addSubview:self.ivDefault];
    [self.view addSubview:self.webView];
    [self.view addSubview:self.navBar];
    [self.view setNeedsUpdateConstraints];
}

#pragma mark - remakeMas
-(void) remakeMas {
    // WeakSelf(self);
    [self.ivDefault mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
    }];
    
    [self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
    }];
    
    [self.navBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@(-VirtualH));
        make.height.equalTo(@44);
    }];
}

#pragma mark - lazy
-(UIImageView *) ivDefault {
    if (!_ivDefault) {
        _ivDefault = [UIImageView new];
        // _ivDefault.backgroundColor = UIColor.whiteColor;
        _ivDefault.contentMode = UIViewContentModeScaleAspectFill;
        
        NSString * path = [NSBundle.mainBundle pathForResource:@"html_loading" ofType:@"gif"];
        
        NSData * dataGif = [NSData dataWithContentsOfFile:path];
        UIImage * imgGif = [UIImage sd_imageWithGIFData:dataGif];
        _ivDefault.image = imgGif;
    }
    return _ivDefault;
}

-(WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) ];
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        
        _webView.navigationDelegate = self;
        _webView.scrollView.delegate = self;

        _webView.backgroundColor = UIColor.clearColor;
    }
    return _webView;
}

-(WebPageNavBar *) navBar {
    if (!_navBar) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, 44);
        _navBar = [[WebPageNavBar alloc] initWithFrame:frame delegate:self];
    }
    return _navBar;
}

#pragma mark - request
-(void) requestURL {
    NSURL * url = [NSURL URLWithString:self.strURL];
    if (!url) {
        return;
    }
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    [self.webView loadRequest:request];
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"didFinishNavigation");
    self.ivDefault.hidden = YES;
}

#pragma mark - WebPageDelegate
-(void) webPageDelegateBack {
    if ([self.webView canGoBack]) {
        [self.webView goBack];
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *) scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;

    if (offsetY == 0) {
        contentOffsetY = offsetY;
        self.navBar.hidden = NO;
        return;
    }

    if ( offsetY > contentOffsetY ) {
        contentOffsetY = offsetY;
        NSLog(@"向下浏览");
        self.navBar.hidden = YES;
        return;
    }

    contentOffsetY = offsetY;
    self.navBar.hidden = NO;
    NSLog(@"向上浏览");
}

@end
